package com.le.tester.javaThink.fifth.chapter14_StreamingProgramming;

import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * 描述：mapToInt,mapToLong,mapToDouble
 */
public class FunctionMap {
    static String[] elements = {"12", "", "23", "45"};

    static Stream<String> testSteam() {
        return Arrays.stream(elements);
    }

    static void test(String descr, Function<String, String> func) {
        System.out.println(descr);
        testSteam().map(func)
                .forEach(System.out::print);
    }

    public static void main(String[] args) {
        test("add brackets", o -> "[" + o + "]");
        System.out.println();
        test("Increment", o -> {
            try {
                return Integer.parseInt(o) + 1 + ",";
            } catch (NumberFormatException e) {
                return o;
            }
        });
        System.out.println();
        test("Replace", o -> o.replace("2", "9"));
        System.out.println();
        test("Take last digit", o -> o.length() > 0 ? o.charAt(o.length() - 1) + "" : o);
    }
}
